{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a79ef54-63e6-429f-ba9f-590b736a7f29",
   "metadata": {},
   "source": [
    "# 3.8 张量的其他运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "- 创建值为[1.5,2,-3]，类型为float32的张量re1\n",
    "- 创建值为[4,5,6]，类型为float32的张量re2\n",
    "- 创建值为[True,True,False]的张量re3\n",
    "- 获取re1的符号，得到张量re4\n",
    "- 获取re1的绝对值，得到张量re5\n",
    "- 获取re1的相反数，得到张量re6\n",
    "- 对re1向上取整，得到张量re7\n",
    "- 对re1向下取整，得到张量re8\n",
    "- 对re1取最接近的元素整数，得到张量re9\n",
    "- 对re1进行四舍五入，得到张量re10\n",
    "- 对re1和re2取对应元素的最大值，得到张量re11\n",
    "- 对re1和re2取对应元素的最小值，得到张量re12\n",
    "- 对re3逻辑取反，得到张量re13"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ad989a-9b82-43e1-b841-e74284cd5936",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "TensorFlow的math模块提供了取符号、取绝对值、取相反数、向上取整、向下取整、取最接近的元素整数、四舍五入、取最大值、取最小值与逻辑取反等功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor([ 1.5  2.  -3. ], shape=(3,), dtype=float32)\n",
      "tf.Tensor([4. 5. 6.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ True  True False], shape=(3,), dtype=bool)\n",
      "tf.Tensor([ 1.  1. -1.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([1.5 2.  3. ], shape=(3,), dtype=float32)\n",
      "tf.Tensor([-1.5 -2.   3. ], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ 2.  2. -3.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ 1.  2. -3.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ 2.  2. -3.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ 2.  2. -3.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([4. 5. 6.], shape=(3,), dtype=float32)\n",
      "tf.Tensor([ 1.5  2.  -3. ], shape=(3,), dtype=float32)\n",
      "tf.Tensor([False False  True], shape=(3,), dtype=bool)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "# 创建值为[1.5,2,-3]，类型为float32的张量re1\n",
    "re1=tf.constant([1.5,2,-3],dtype=tf.float32)\n",
    "print(re1)\n",
    "# 创建值为[4,5,6]，类型为float32的张量re2\n",
    "re2=tf.constant([4,5,6],dtype=tf.float32)\n",
    "print(re2)\n",
    "# 创建值为[True,True,False]的张量re3\n",
    "re3=tf.constant([True,True,False])\n",
    "print(re3)\n",
    "# 获取re1的符号，得到张量re4\n",
    "re4=tf.math.sign(re1)\n",
    "print(re4)\n",
    "# 获取re1的绝对值，得到张量re5\n",
    "re5=tf.math.abs(re1)\n",
    "print(re5)\n",
    "# 获取re1的相反数，得到张量re6\n",
    "re6=tf.math.negative(re1)\n",
    "print(re6)\n",
    "# 对re1向上取整，得到张量re7\n",
    "re7=tf.math.ceil(re1)\n",
    "print(re7)\n",
    "# 对re1向下取整，得到张量re8\n",
    "re8=tf.math.floor(re1)\n",
    "print(re8)\n",
    "# 对re1取最接近的元素整数，得到张量re9\n",
    "re9=tf.math.rint(re1)\n",
    "print(re9)\n",
    "# 对re1进行四舍五入，得到张量re10\n",
    "re10=tf.math.round(re1)\n",
    "print(re10)\n",
    "# 对re1和re2取对应元素的最大值，得到张量re11\n",
    "re11=tf.math.maximum(re1,re2)\n",
    "print(re11)\n",
    "# 对re1和re2取对应元素的最小值，得到张量re12\n",
    "re12=tf.math.minimum(re1,re2)\n",
    "print(re12)\n",
    "# 对re3逻辑取反，得到张量re13\n",
    "re13=tf.math.logical_not(re3)\n",
    "print(re13)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f2c61db-4b69-49f9-9b22-f6d2642a056d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
